iT邦幫忙

2022 iThome 鐵人賽

DAY 7
0

##JS basic

  • JS30- day06- AJX, fetch()
const cities =[]

fetch(endpoint) -> 返回promise而非data
    .then(blob => blob.json()) 再返回一個promise
    .then(data => cities.push(...data))
    

function findMatches(wordToMatch, cities){
    return cities.filter(place => {
        con regex = new RegExp(wordToMatch, 'gi')
        return place.city.match(regex) || place.state.match(regex)
    })
}
function numberWithCommas(x){
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
function displayMatches(){
    const matchArray = findMatches(this.value, cities)
    const html = matchArray.map(place => {
        const regex = new RegExp(this.value, 'gi')
        const cityName = place.city.replace(regex, `<span class = 'h1'>${this.value}</span>`
        const stateName = place.state.replace(regex, `<span class = 'h1'>${this.value}</span>`
        return `
            <li>
                <span class='name'>${cityName}, ${stateName}</span>
                <span class='population'>${numberWithCommas(place.population)}</span>
            </li>
        `
    }).join('')
    suggestions.innerHTML = html
}

const searchInput = document.querySelector('.search')
const suggestion = document.querySelector('.suggestions')

searchInput.addEventListener('changed', displayMatches)
searchInput.addEventListener('keyup', displayMatches)

##前端

  • 實作React讀書會 Week1 作業level 1
    ##後端
  • php + mysql 課程

上一篇
Day05- FlexBox, JWT複習, picoCTF續戰
下一篇
Day07- Array, laravel, 閱讀其他鐵人賽
系列文
雜學海綿吸呀吸 Dev Diary10
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言